import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { resolve } from 'path';
// 导入 postcss-px-to-viewport 和 autoprefixer
import pxtoviewport from 'postcss-px-to-viewport';
import autoprefixer from 'autoprefixer';

// https://vite.dev/config/
export default defineConfig({
  css: {
    loaderOptions: {
      postcss: {
        postcssOptions: {
          plugins: [
            pxtoviewport({
              unitToConvert: 'px',
              viewportWidth: 750,
              unitPrecision: 3,
              propList: ['*'],
              viewportUnit: 'vw',
              fontViewportUnit: 'vw',
              minPixelValue: 2,
              mediaQuery: true,
              replace: true,
              exclude: [/node_modules/]
            }),
            autoprefixer()
          ]
        }
      }
    }
  },
  plugins: [
    vue(),
    vueDevTools(),
    createSvgIconsPlugin({//svg图标
      iconDirs:[resolve(process.cwd(), 'src/assets/svg')],
      symbolId: 'icon-[dir]-[name]',
    }),
  ],
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: []
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://81.71.154.186:8088/api',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
    },
    host: '0.0.0.0', // 这个用于启动
    port: '8090', // 指定启动端口
    open: true, //启动后是否自动打开浏览器
  }
})
